//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ZeroCouponInflationSwapHelper.h"
using namespace Cephei::QL::Termstructures::Inflation;
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Termstructures/ZeroInflationTermStructure.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (Cephei::QL::IQuote^ quote, Cephei::QL::Times::IPeriod^ swapObsLag, DateTime maturity, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Indexes::IZeroInflationIndex^ zii) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CQuote^ _Cquote;
    CPeriod^ _CswapObsLag;
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    CZeroInflationIndex^ _Czii;
    try
    {
#ifdef HANDLE
        _phZeroCouponInflationSwapHelper = NULL;
#endif
        _Cquote = safe_cast<CQuote^> (quote);
        _Cquote->Lock();
        Handle<QuantLib::Quote>& _quote = static_cast<Handle<QuantLib::Quote>&> (_Cquote->GetHandle ()); 
        _CswapObsLag = safe_cast<CPeriod^> (swapObsLag);
        _CswapObsLag->Lock();
        QuantLib::Period& _swapObsLag = static_cast<QuantLib::Period&> (_CswapObsLag->GetReference ()); 
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _Czii = safe_cast<CZeroInflationIndex^> (zii);
        _Czii->Lock();
        boost::shared_ptr<QuantLib::ZeroInflationIndex>& _zii = static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>&> (_Czii->GetShared ()); 
        _ppZeroCouponInflationSwapHelper = new boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper> (new QuantLib::ZeroCouponInflationSwapHelper ( _quote,  _swapObsLag,  _maturity,  _calendar,  _paymentConvention,  _dayCounter,  _zii ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cquote != nullptr) _Cquote->Unlock();
        if (_CswapObsLag != nullptr) _CswapObsLag->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Czii != nullptr) _Czii->Unlock();
    }
}
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZeroCouponInflationSwapHelper = NULL;
#endif
	_ppZeroCouponInflationSwapHelper = &childNative;
    
}
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (QuantLib::ZeroCouponInflationSwapHelper& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phZeroCouponInflationSwapHelper = NULL;
#endif
	_ppZeroCouponInflationSwapHelper = new boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper> (&childNative);
    
    _ZeroCouponInflationSwapHelperOwner = owner;
    
}

Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (CZeroCouponInflationSwapHelper^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZeroCouponInflationSwapHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppZeroCouponInflationSwapHelper = new boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZeroCouponInflationSwapHelper = NULL;
#endif
	if (!t->IsSubclassOf(CZeroCouponInflationSwapHelper::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (QuantLib::Handle<QuantLib::ZeroCouponInflationSwapHelper>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phZeroCouponInflationSwapHelper = &childNative;
	_ppZeroCouponInflationSwapHelper = &static_cast<boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper>>(childNative.currentLink());
    
    _ZeroCouponInflationSwapHelperOwner = owner;
}
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (QuantLib::Handle<QuantLib::ZeroCouponInflationSwapHelper> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phZeroCouponInflationSwapHelper = &childNative;
	_ppZeroCouponInflationSwapHelper = &static_cast<boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::CZeroCouponInflationSwapHelper (QuantLib::ZeroCouponInflationSwapHelper childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phZeroCouponInflationSwapHelper = NULL;
#endif
	_ppZeroCouponInflationSwapHelper = new boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper> (new QuantLib::ZeroCouponInflationSwapHelper (childNative));
    
}
#endif

Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::~CZeroCouponInflationSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppZeroCouponInflationSwapHelper != NULL)
    {
	    delete _ppZeroCouponInflationSwapHelper;
        _ppZeroCouponInflationSwapHelper = NULL;
    }
}
Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::!CZeroCouponInflationSwapHelper ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppZeroCouponInflationSwapHelper != NULL)
    {
	    delete _ppZeroCouponInflationSwapHelper;
    }
}
QuantLib::ZeroCouponInflationSwapHelper& Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::GetReference ()
{
    if (_ppZeroCouponInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return **_ppZeroCouponInflationSwapHelper;
}
boost::shared_ptr<QuantLib::ZeroCouponInflationSwapHelper>& Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::GetShared ()
{
    if (_ppZeroCouponInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return *_ppZeroCouponInflationSwapHelper;
}
QuantLib::ZeroCouponInflationSwapHelper* Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::GetPointer ()
{
    if (_ppZeroCouponInflationSwapHelper == NULL) throw REFNEW NativeNullException ();
	return &**_ppZeroCouponInflationSwapHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ZeroCouponInflationSwapHelper>& Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::GetHandle ()
{
	if (_phZeroCouponInflationSwapHelper == NULL)
	{
		_phZeroCouponInflationSwapHelper = new Handle<QuantLib::ZeroCouponInflationSwapHelper> (*_ppZeroCouponInflationSwapHelper);
	}
	return *_phZeroCouponInflationSwapHelper;
}
#endif
bool Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::HasNative () 
{
	return (_ppZeroCouponInflationSwapHelper != NULL);
}

Double Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::ImpliedQuote::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppZeroCouponInflationSwapHelper)->impliedQuote ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Inflation::IZeroCouponInflationSwapHelper^ Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper::SetTermStructure (Cephei::QL::Termstructures::IZeroInflationTermStructure^ z)
{
    CZeroInflationTermStructure^ _Cz;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cz = safe_cast<CZeroInflationTermStructure^> (z);
        _Cz->Lock();
        QuantLib::ZeroInflationTermStructure* _z = static_cast<QuantLib::ZeroInflationTermStructure*> (_Cz->GetPointer ()); 
    	(*_ppZeroCouponInflationSwapHelper)->setTermStructure ( _z );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cz != nullptr) _Cz->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Inflation::IZeroCouponInflationSwapHelper^ Cephei::QL::Termstructures::Inflation::CZeroCouponInflationSwapHelper_Factory::Create (Cephei::QL::IQuote^ quote, Cephei::QL::Times::IPeriod^ swapObsLag, DateTime maturity, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Indexes::IZeroInflationIndex^ zii)
{
    return REFNEW CZeroCouponInflationSwapHelper ( quote,  swapObsLag,  maturity,  calendar,  paymentConvention,  dayCounter,  zii);
}
